

import java.io.IOException;
import java.sql.*;
import java.sql.Date;
import java.util.*;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class Movement
 */
@WebServlet("/Movement")
public class Movement extends HttpServlet {
	private static final long serialVersionUID = 1L;

    public Movement() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession ses = request.getSession();
		Integer USER_ID = (Integer) ses.getAttribute("USER_ID");
		Integer P_ADDRESS = (Integer) ses.getAttribute("P_ADDRESS");
		
		if(USER_ID==null||P_ADDRESS==null){
			response.sendRedirect("Overview");
			return;
		}
		Database data = null;
		ResultSet rs = null;
		ArrayList<Integer> MOV_ID = new ArrayList<>();
		ArrayList<Timestamp> START_TIME = new ArrayList<>();
		ArrayList<Timestamp> ARRIVAL_TIME = new ArrayList<>();
		ArrayList<Integer> ARMY_ID = new ArrayList<>();
		ArrayList<Integer> RES_ID = new ArrayList<>();
		ArrayList<Integer> QUANTITY = new ArrayList<>();
		ArrayList<Integer> SOURCE_ADDRESS = new ArrayList<>();
		ArrayList<Integer> DESTINATION_ADDRESS = new ArrayList<>();
		ArrayList<Integer> TARGET_ARMY_ID = new ArrayList<>();
		ArrayList<Integer> TARGET_RES_ID = new ArrayList<>();
		ArrayList<Integer> TARGET_USER_ID = new ArrayList<>();
		ArrayList<Integer> SOURCE_USER_ID = new ArrayList<>();
		ArrayList<String> TARGET_NAME = new ArrayList<>();
		try {
			data = new Database();
			rs = data.Select("*").From("MOVEMENTS").Where("SOURCE_ADDRESS = " + P_ADDRESS).OrderBy("ARRIVAL_TIME").GetResult();
			
			while(rs.next()){
				MOV_ID.add(rs.getInt("MOV_ID"));
				START_TIME.add(rs.getTimestamp("START_TIME"));
				ARRIVAL_TIME.add(rs.getTimestamp("ARRIVAL_TIME"));
				ARMY_ID.add(rs.getInt("ARMY_ID"));
				RES_ID.add(rs.getInt("RES_ID"));
				QUANTITY.add(rs.getInt("QUANTITY"));
				SOURCE_ADDRESS.add(rs.getInt("SOURCE_ADDRESS"));
				DESTINATION_ADDRESS.add(rs.getInt("DESTINATION_ADDRESS"));
				TARGET_ARMY_ID.add(rs.getInt("TARGET_ARMY_ID"));
				TARGET_RES_ID.add(rs.getInt("TARGET_RES_ID"));
				TARGET_USER_ID.add(rs.getInt("TARGET_USER_ID"));
				SOURCE_USER_ID.add(rs.getInt("USER_ID"));
			}
			rs.close();
			data.Close();
			Iterator<Integer> iter = TARGET_USER_ID.iterator();
			
			while(iter.hasNext()){
				Integer uid = iter.next();
				data = new Database();
				rs = data.Select("NAME").From("G_USER").Where("USER_ID = " + uid).GetResult();
				if(rs.next()){
					TARGET_NAME.add(rs.getString("NAME"));
				}
				rs.close();
				data.Close();
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		request.setAttribute("MOV_ID", MOV_ID);
		request.setAttribute("SOURCE_USER_ID", SOURCE_USER_ID);
		request.setAttribute("START_TIME", START_TIME);
		request.setAttribute("ARRIVAL_TIME", ARRIVAL_TIME);
		request.setAttribute("QUANTITY", QUANTITY);
		request.setAttribute("SOURCE_ADDRESS", SOURCE_ADDRESS);
		request.setAttribute("DESTINATION_ADDRESS", DESTINATION_ADDRESS);
		request.setAttribute("T_USER_ID", TARGET_USER_ID);
		request.setAttribute("TARGET_NAME", TARGET_NAME);
		
		request.getRequestDispatcher("Movement.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
